<?php
    /*
     * This program is part of BSCAH, which is free software.  It comes with
     * absolutely no warranty. You can redistribute and/or modify it under the terms
     * of the GNU General Public License as published by the Free Software Foundation
     * (see <http://www.gnu.org/licenses/ for more information).
     *
     *
     * @author Matthew Freitas
     * @version 2014-12-15
     * The new frontend for the Master Calendar
     */

    session_start();
    session_cache_expire(30);

    include_once("fullcalendar.inc");
    include_once("mastercalendar_full.inc");
?>
<!--  page generated by the BowdoinRMH software package -->
<html>
    <head>
        <title><?php echo ucfirst($_GET['frequency']); ?> Master Schedule</title>
        <link rel="stylesheet" href="styles.css" type="text/css"/>
        <link rel="stylesheet" href="calendarhouse.css" type="text/css"/>
        <script type="text/javascript">
            /**
             * Creates the FullCalendar object in <div id="calendar"> HTML element
             * @param json The JSON returned from the PHP function "get_fullcalendar_json(Week $week)"
             */
            function makeCalendar(json) {
                // WARNING: Please always use "jQ2" to refer to jQuery instead of "$". There is a conflict in our project; "$" is jQuery 1.9
                jQ2(document).ready(function() {
                    // The options that are available to you are detailed in the FullCalendar.io docs: http://fullcalendar.io/docs/
                    // All FullCalendar customization is done simply by attaching a new field to this JSON
                    jQ2("#calendar").fullCalendar({
                        events: json,

                        editable: false,

                        defaultView: 'agendaWeek',
                        defaultDate: moment().format(),

                        // These are magic strings, check: http://fullcalendar.io/docs/display/header/ for valid inputs
                        header: {
                            left: '',
                            center: '',
                            right: ''
                        },

                        allDaySlot: false,
                        minTime: '09:00:00',
                        maxTime: '22:00:00', // We set this to end at 10PM, because we are going to rename the 9-10PM slot to "overnight"

                        eventClick: function(event, jsEvent, view) {
                            showModal(event, jsEvent, view);
                        },

                        eventAfterAllRender: function(view) {
                            reflowCalendar(view);

                            //Remove date information
                            jQ2(".fc-day-header.fc-widget-header").each(function() {
                                jQ2(this).text(jQ2(this).text().substring(0,3));
                            });
                        }
                    })
                })
            }
        </script>
    </head>
    <body>
        <div id="container">
            <?php
                echo "<a href=\"index.php\">";
                include_once("header.php");
                echo "</a>";
                include_once('accessController.php');
            ?>
            <div id="content" style="text-align:center">
                <?php
                    if ($_SESSION['access_level'] < 2) {
                        die("<p>Only managers can view the master schedule.</p>");
                    }
                    $venue = $_GET['frequency'];
                ?>
                <center><h2><?php echo ucfirst($venue) . ' Calendar'?></h2></center>
                <a onclick='jQ2("#calendar-modal").modal();'>Manage Shifts</a>
                <script type="text/javascript">
                    makeCalendar(<?php echo get_fullcalendar_json($venue); ?>);
                </script>
                <div id="calendar"></div>
            </div>
            <!-- Bootstrap modal -->
            <div id="calendar-modal" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
                <div class="modal-dialog modal-sm">
                    <div class="modal-content">
                        <?php include_once("addMasterShiftForm.inc") ?>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <?php include_once("footer.inc"); ?>
</html>
